﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using GSQ.BusinessEntities;
using GSQ.DataAccess.DataSetTableAdapters;

namespace GSQ.DataAccess
{
    public class RutasDA
    {
        private RUTASTableAdapter Adapter = new RUTASTableAdapter();
        private DataSet.RUTASDataTable entities;
        private DataSet.RUTASRow entity;

        public decimal Insert(RUTAS value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    Adapter.Connection = cnn;

                    entities = new DataSet.RUTASDataTable();

                    entity = entities.NewRUTASRow();

                    entity.ID = value.ID;
                    entity.RUTA = value.RUTA.Trim();
                    entity.COMENTARIO = value.COMENTARIO;

                    entities.AddRUTASRow(entity);
                    Adapter.Update(entities);

                    value.ID = entity.ID;
                    return value.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public decimal Update(RUTAS value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    entities = Adapter.GetDataByID(value.ID);
                    entity = entities.FirstOrDefault();


                    entity.RUTA = value.RUTA.Trim();
                    entity.COMENTARIO = value.COMENTARIO;

                    this.Adapter.Update(entities);

                    return entity.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public RUTAS GetById(decimal id)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    RUTAS banco;

                    entity = this.Adapter.GetDataByID(id).FirstOrDefault();

                    if (entity != null)
                    {
                        banco = new RUTAS
                            {
                                ID = entity.ID,
                                RUTA = entity.RUTA,
                                COMENTARIO = entity.COMENTARIO
                            };
                    }
                    else
                        banco = null;

                    return banco;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<RUTAS> GetAll()
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<RUTAS> lst = new List<RUTAS>();
                    foreach (DataSet.RUTASRow rw in this.Adapter.GetData())
                    {
                        lst.Add(new RUTAS
                        {
                            ID = rw.ID,
                            RUTA = rw.RUTA,
                            COMENTARIO = rw.COMENTARIO
                        });
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<RUTAS> GetByDescripcion(string value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<RUTAS> lst = new List<RUTAS>();
                    foreach (DataSet.RUTASRow rw in this.Adapter.GetDataByRuta(value.Trim()))
                    {
                        lst.Add(new RUTAS
                        {
                            ID = rw.ID,
                            RUTA = rw.RUTA,
                            COMENTARIO = rw.COMENTARIO
                        });
                    }
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }
    }
}
